package scribes.mybus.ejb;


import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



import scribes.mybus.data.bus;


/**
 * Session Bean implementation class HelloProjectBeanEjb
 */
@Remote(MyBusProjectBean.class)
@Stateless

public class MyBusProjectEJBBean implements MyBusProjectBean {

	@PersistenceContext(unitName = "wmbDb")
	private EntityManager data;
	@Resource
	private SessionContext ctx;
    /**
     * Default constructor. 
     */
    public MyBusProjectEJBBean() {
        // TODO Auto-generated constructor stub
    }

	

	@Override
	public String getCurrentBusInfo(int busId, String desiredOutputFormat) {
		// TODO Auto-generated method stub
		try{
			System.out.println("In getCurrentBusInfo call");
			
			Query querygetBusData = data.createNamedQuery("getbusdata");
			//querygetBusData.setParameter("id", busId);
		
			List<bus> objbus = (List<bus>) querygetBusData.getResultList();
			
			if(objbus.size()>=1) //objbus != null)
			{
				System.out.println("Found Bus details");
				
				List <BusInfoOutput> businfoOutput = (List<BusInfoOutput>) new BusInfoOutput(objbus);
				
				String outputString = "";
				OutputFormatInterface objOutputFormat;
				if("jsonOuputFormat".equals(desiredOutputFormat)){
					objOutputFormat = new JSONOutput();
				} else if("kmlOutputFormat".equals(desiredOutputFormat)){
					objOutputFormat = new KMLOutput();
				} else {
					System.out.println(desiredOutputFormat + " Output Format is not Supported!");
					return null;
				}
				outputString = objOutputFormat.parseObject(List<businfoOutput>);
				System.out.println(outputString);
				return  outputString;
			}
			else
				return null;		
			}
			catch (Exception e){
				e.printStackTrace();			
			}
			return null;
	}

	@Override
	public void uploadFile(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("File received");
		System.out.println("filename" +fileName);
		sendMessage(fileName);
		
	}
	
	public void sendMessage(String filename)
	{
		QueueSession session = null;
		QueueSender sender = null;
		QueueConnection cnn = null;
		try {
			
			System.out.println(ctx);
			Queue queue = (Queue) ctx.lookup("scribes/logging");
			QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			cnn = factory.createQueueConnection();
			session = cnn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
			sender = session.createSender(queue);
			MapMessage m = session.createMapMessage();
			m.setString("fileName", filename);
			sender.send(m);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				sender.close();
				session.close();
				cnn.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}

